Ext.define('EUF.controller.EUForm', {
    extend: 'Ext.app.Controller',
	
	views: ['piechart','newStudent','StudentList', 'DuplicateSlip', 'FeeDetailsForm',
			'FeeStatement', 'HeadWiseDetails', 'StudentDetailsView', 'StudentForm',
			'RoleSearch', 'EyeView', 'Status','ClassMasterView', 'login'
    ],
	
	models :['StudentInfoMdl','SubmitRequestMdl','UserAccessMdl','UserDetailMdl',
			 'UserGroupModel', 'UserParametersModel', 'UserSystemModel','PriorityMdl',
			 'RequestTypeMdl'
	],
	
	stores :['StudentStore','SubmitRequestStore','UserDetailStore','UserGroupStore',
			 'UserParametersStore','UserSystemStore', 'PriorityStore','RequestTypeStore',
			 'ClassStore','FeeStore', 'PieClassStore'
	],
	
	userModel:null,
	me : null,
    init: function() {
	me  = this,
	this.userModel = Ext.create('EUF.model.SubmitRequestMdl');
	this.userModel.set('notes','Enter Your Address');
	var classStore = this.getClassStoreStore();
	classStore.load(function(records, operation, success) {
	var classRecords = records;
    console.log('loaded records');
	console.log(records.length);
	var studentStore = me.getStudentStoreStore();
	studentStore.load(function(records, operation, success) {
	var chartStore = me.getPieClassStoreStore() ;
	console.log(chartStore);
	
   	for(var i = 0;i<classRecords.length;i++)
	{
	console.log(classRecords[i]);
	studentStore.filter('className',classRecords[i].data.className);
	console.log(studentStore.count());
	if(studentStore.count()>0)
	chartStore.add({name:classRecords[i].data.className, data1:studentStore.count()});
	studentStore.clearFilter();
	}	
	
	console.log(me.getPieClassStoreStore());
	
	
});
});
	//this.userModel.set('firstname','harsh');
//	this.userModel.set('lastname','sharma');
	
	 
        this.control({
        	'gridpanel': {
    		 	afterrender: function (grid) {
    		        grid.relayEvents(grid.getStore(), ['datachanged'])
    		    }
    		    , datachanged: function (store, records) {
    		    	
    		    	//alert("dsgds");
    		    	sysAdded = true;    		    	
    		    	console.log(store.count());
		    		var sys = '';
    		    	for(var i=0;i<store.count();i++){
    		    		var system = store.getAt(i).data.system;
    		    		if(system != undefined && system != null && system != ""){
    		    			if(i==0)
        		    			sys = system;
        		    		else
        		    			sys=sys+','+system;
    		    		}    		    		
    		    	}
    		    	//Ext.getCmp('sysAdded').setValue(sys);
    		    }
    		    , update: function (store, model, operation, eOpts){
    		    	alert("model update...");
    		    }
    		    
        	},
        	
        	
        	'#feesave' : {
        		click : this.saveFee
        	},
        	'#classSaveBtn' : {
        		
        		click : this.saveClassMaster
        	},
        	'#studentSave':{
        		click: this.submitRequest
        	},
            '#header': {
                render: this.onPanelRendered
            },
			'#UserSystem':{
				click : this.openUserSystem
			},
			'#userParameters':{	
				click :this.openUserParameters
			},
			'#userGroup':{
				click :this.openUserGroup
			},
			'#userDetails':{
				click : this.openUserDetails
			},
			'#organization':{	
				click :this.openOrganization
			},
			'#accountinfo':{
				click :this.openAccountInfo
			},
			'#userPersonalDetails': {
                render: this.onUserDetailsRendered
            },
            '#show': {
                click: this.onStudentSearch
            },
            '#reloadPIE': {
                click: this.onReloadPIE
            }
			
        });
    },
	onReloadPIE: function() {
		//alert('reload');
		var chartStore = me.getPieClassStoreStore() ;
		var classStore = me.getClassStoreStore();
		var studentStore = me.getStudentStoreStore();
		var chartStoreList = [];
		
		console.log(studentStore);
		console.log(classStore);
		
	   	for(var i = 0;i<classStore.count();i++)
		{
		//console.log(classRecords[i]);
		studentStore.filter('className',classStore.data.items[i].data.className);
		console.log(studentStore.count());
		if(studentStore.count()>0)
		{
		chartStoreList.push({name:classStore.data.items[i].data.className, data1:studentStore.count()});
		chartStore[i]= {name:classStore.data.items[i].data.className, data1:studentStore.count()};
			console.log(classStore.data.items[i].data.className);
			console.log(studentStore.count());

		}
		//chartStore.add({name:classStore.data.items[i].className, data1:studentStore.count()});
		studentStore.clearFilter();
		}	
		me.getPieClassStoreStore().loadData(chartStoreList);
		console.log('refresh');
		console.log(me.getPieClassStoreStore());
		//me.getPieClassStoreStore()  = chartStore;
		 var chart = Ext.getCmp('chartCmp');
	    chart.refresh();
		
	},
    onStudentSearch: function() {
    	//alert('dd');
    		var batch = Ext.getCmp('stBatch').getValue();
    console.log(batch);
 	var studentName = Ext.getCmp('studentName').getValue();
    console.log(studentName);
    var whereFieldParam;
    if(studentName!='')
    whereFieldParam  = "[{'studentName':'"+studentName+"'}]";

    if(batch!='')
        whereFieldParam  = "[{'batch':'"+batch+"'}]";

    if(studentName!='' && batch!='')
        whereFieldParam  = "[{'studentName':'"+studentName+"'},{'batch':'"+batch+"'}]";

       

        	
  /*  if(batch!='')
        whereFieldParam  = whereFieldParam +"','batch':'"+batch+"'}]";
        */
    
  //  whereFieldParam = whereFieldParam + 
    
   // whereFieldParam = Ext.JSON.encode(whereFieldParam);
    
   // whereFieldParam =  "[{'studentName':'abhi','batch':'34'}]";
    
    this.getStudentStoreStore().getProxy().extraParams.whereFields=whereFieldParam;
    this.getStudentStoreStore().load();
    	
    	
    },
	onUserDetailsRendered: function() {
	 var userFormHeader = Ext.getCmp('userPersonalDetails');//later try this.getView
		if(userFormHeader!= undefined)
		{
		 console.log('found user form');
		 console.log(userFormHeader.form);
		  userFormHeader.form.loadRecord(this.userModel);
	
		}
		
    
    
    },
    
    
    saveFee : function() {

    	var feeMdl = Ext.create('EUF.model.FeeMdl');
    	var feeForm = Ext.getCmp('feeDetailsForm');//later try this.getView
    	feeForm.getForm().updateRecord(feeMdl);
   		console.log(feeMdl);
   		feeMdl.save();
   		me.getFeeStoreStore().load();
    },
    
    saveClassMaster : function() {

    	var classModel = Ext.create('EUF.model.ClassMasterMdl');
    	var classMasterForm = Ext.getCmp('ClassMasterView');//later try this.getView
    	classMasterForm.getForm().updateRecord(classModel);
   		console.log(classModel);
   		classModel.save();
   		me.getClassStoreStore().load();
    }
	,
	
	submitRequest: function(button){
	
	var studentModel = Ext.create('EUF.model.StudentInfoMdl');

	 var studentFormHeader = button.up('form'); //Ext.getCmp('newStudentForm');//later try this.getView
	 studentFormHeader.getForm().updateRecord(studentModel);
		console.log(studentModel);
	studentModel.save();
//this.userModel.set('notes','Enter Your Address');
//	this.userModel.set('firstname','harsh');
//	this.userModel.set('lastname','sharma');
	
	
	
	
	
		var msgArray = new Array();
		msgArray.push('images/icons/icon.information.green.png');
		var msg = Ext.create('fanta.Messages');
       	 msg.getMessage('Student Saved Succesfully', msgArray);
	},
    onPanelRendered: function() {
	var formHeader = Ext.getCmp('header');//later try this.getView
	//  var formHeader = this.getView('userPersonalDetails');//later try this.getView
	
	 console.log(formHeader);
     console.log(this.userModel);
	 if(formHeader!=undefined)
	 {
	 formHeader.form.loadRecord(this.userModel);
	    console.log('The panel was rendered');
		
		
	}
    
    }
	,

    /*openUserDetails: function() {
	var userWin = Ext.create('Ext.window.Window',
	{	
	title : 'user details',
	items : {xtype:'userpersonaldetails'},
	width : 900,
	height : 550,
	animateTarget:'header'
	});
	userWin.show();
	
    },
	openOrganization: function() {
	Ext.create('Ext.window.Window',
	{	
	title : 'Organization',
	items : {xtype:'userorganizationdetails'},
	width : 900,
	height : 550,
	animateTarget:'organization'
	}).show();
        console.log('The user details was rendered');
    },
	openAccountInfo: function() {
	Ext.create('Ext.window.Window',
	{	
	title : 'Account Info',
	items : {xtype:'useraccountdetails'},
	width : 900,
	height : 550,
	animateTarget:'header'
	}).show();
		console.log('The user details was rendered');
    },
	openUserSystem: function() {
	Ext.create('Ext.window.Window',
	{	
	title : 'System',
	items : {xtype:'usersystem'},
	width : 900,
	height : 550,
	animateTarget:'organization'
	}).show();
        console.log('The user details was rendered');
    },
	openUserParameters: function() {
	Ext.create('Ext.window.Window',
	{	
	title : 'Parameters',
	items : {xtype:'userparameters'},
	width : 900,
	height : 550,
	animateTarget:'header'
	}).show();
		console.log('The user details was rendered');
	},
	openUserGroup: function() {
	Ext.create('Ext.window.Window',
	{	
	title : 'User Group',
	items : {xtype:'usergroup'},
	width : 900,
	height : 550,
	animateTarget:'header'
	}).show();
        console.log('The accountinfo was rendered');
    }*/
});